package array;


import java.util.Scanner;

/**
 * https://blog.csdn.net/qq_40703471/article/details/123855881
 */
public class MinSubArrayLen {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("nums = ");
        String s = sc.nextLine();
        String[] split = s.substring(1, s.length() - 1).split(",");
        int[] nums = new int[split.length];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = Integer.parseInt(split[i]);
        }
        System.out.println("target = ");
        int target = sc.nextInt();
        System.out.println(minSubArrayLen(nums, target));
    }
    public static int minSubArrayLen(int[] nums, int target){
        int left = 0;
        int sum = 0;
        int res = Integer.MAX_VALUE;
        for (int right = 0; right < nums.length; right++){
            sum += nums[right];
            while (sum >= target){
                res = Math.min(res, right - left + 1);
                sum -= nums[left];
                left++;
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}
